package com.example.core.extensions

import kotlin.properties.ReadOnlyProperty
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
 * Created by zhudongao
 * Desc
 * Date 2020/9/19
 */
private class NotNullSingleValue<T> : ReadWriteProperty<Any?, T> {
    private var value: T? = null

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        if (this.value == null) {
            this.value = value
        } else {
            throw IllegalStateException("already initialized!")
        }
    }

    override fun getValue(thisRef: Any?, property: KProperty<*>): T {
        return value ?: throw IllegalStateException("application not initialized!")
    }

}

fun <T> notNullSingleValue():ReadWriteProperty<Any?,T> = NotNullSingleValue()
